//
// Created by hkkzzxz on 24-9-4.
//

#include "SuperAdminController.h"
#include "SuperAdminService.h"

void SuperAdminController::addAdmin(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                    http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    Json::Value srcRoot;

    //尝试解析字符串
    if (parseRequestBody(httpRequest, srcRoot)) {
        auto id = srcRoot["id"].asString();
        auto name = srcRoot["name"].asString();
        auto password = srcRoot["password"].asString();

        std::unique_ptr<ISuperAdminService> superAdminService = std::make_unique<SuperAdminService>();
        bool result = superAdminService->addAdmin(id, name, password);

        if (result) {
            httpResponse.result(http::status::ok);
            root["code"] = 0;
            root["message"] = "Successful add admin";
        } else {
            httpResponse.result(http::status::bad_request);
            root["code"] = 1002;
            root["message"] = "Failed add admin";
        }
    } else {
        httpResponse.result(http::status::bad_request);
        root["code"] = 1001;
        root["message"] = "Failed to parse JSON data!";
    }
    root["data"] = srcRoot;

    setHttpResponse(httpResponse, root);
}

void SuperAdminController::deleteAdmin(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                       http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    Json::Value srcRoot;

    //尝试解析字符串
    if (parseRequestBody(httpRequest, srcRoot)) {
        auto id = srcRoot["id"].asString();
        auto name = srcRoot["name"].asString();
        auto password = srcRoot["password"].asString();

        std::unique_ptr<ISuperAdminService> superAdminService = std::make_unique<SuperAdminService>();
        bool result = superAdminService->deleteAdmin(id, name);

        if (result) {
            httpResponse.result(http::status::ok);
            root["code"] = 0;
            root["message"] = "Successful delete admin";
        } else {
            httpResponse.result(http::status::bad_request);
            root["code"] = 1002;
            root["message"] = "Failed delete admin";
        }
    } else {
        httpResponse.result(http::status::bad_request);
        root["code"] = 1001;
        root["message"] = "Failed to parse JSON data!";
    }
    root["data"] = srcRoot;

    setHttpResponse(httpResponse, root);
}

void SuperAdminController::checkAdmin(std::string &data, http::request<http::dynamic_body> &httpRequest,
                                      http::response<http::dynamic_body> &httpResponse) {
    Json::Value root;
    std::unique_ptr<ISuperAdminService> superAdminService = std::make_unique<SuperAdminService>();
    auto result = superAdminService->checkAdmin();
    httpResponse.result(http::status::ok);
    root["code"] = 0;
    root["data"] = result;
    root["message"] = "Successful check admin information";
    setHttpResponse(httpResponse, root);
}
